package online.task;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @Auther: 狼芒
 * @Date: 2021/10/5 1:13
 * @Descrintion: 定时任务
 * @version: 1.0
 */
@Component
public class SchedulPoolTask {

    private Boolean stopStartSch = true;

    /**
     * 三秒执行一次
     */
    @Scheduled(fixedRate = 3 * 1000)
    public void play() {
        if (stopStartSch) {
            return;
        }
        System.out.println("SchedulPoolTask --> play");
    }

    /**
     * 五秒执行一次
     */
    @Scheduled(cron = "0/5 * * * * ?")
    public void say() {
        if (stopStartSch) {
            return;
        }
        System.out.println("SchedulPoolTask --> say");
    }

    /**
     * 异步任务执行
     */
    @Async
    @Scheduled(cron = "0/5 * * * * ?")
    public void doSomthing() {
        if (stopStartSch) {
            return;
        }
        System.out.println("线程：" + Thread.currentThread().getName() + "SchedulPoolTask --> doSomthing 时间：" + LocalDateTime.now());
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
